package hasthi.tools.analyze;

import hasthi.common.HasthiException;
import hasthi.common.Utils;
import hasthi.common.HasthiException.FaultCode;
import hasthi.manager.ManagerContextDocument;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.xmlbeans.XmlObject;

public class XSLTTransformer {
    public static void transformWithXSLT(XmlObject source,String file,OutputStream out) throws HasthiException{
        try {
            ByteArrayOutputStream tempout = new ByteArrayOutputStream();
            source.save(tempout);
            tempout.flush();
            ByteArrayInputStream inputStream = new ByteArrayInputStream(tempout.toByteArray());
            // Create transformer factory
            TransformerFactory factory = TransformerFactory.newInstance();

            // Use the factory to create a template containing the xsl file
            Templates template = factory.newTemplates(new StreamSource(
                new FileInputStream(file)));

            // Use the template to create a transformer
            Transformer xformer = template.newTransformer();

            // Prepare the input and output files
            Source inSource = new StreamSource(inputStream);
            
            StreamResult result = new StreamResult(out);
            
            // Apply the xsl file to the source file and write the result to the output file
            xformer.transform(inSource, result);
        } catch (TransformerConfigurationException e) {
            throw new HasthiException(e,FaultCode.LocalError);
        } catch (FileNotFoundException e) {
            throw new HasthiException(e,FaultCode.LocalError);
        } catch (IOException e) {
            throw new HasthiException(e,FaultCode.LocalError);
        } catch (TransformerFactoryConfigurationError e) {
            throw new HasthiException(e,FaultCode.LocalError);
        } catch (TransformerException e) {
            throw new HasthiException(e,FaultCode.LocalError);
        }
    }
    
    public static void main(String[] args) throws Exception {
//        ManagerContextDocument managerContextDocument = ManagerContextDocument.Factory.newInstance();
//        ManagementRing managementRing = managerContextDocument.addNewManagerContext().addNewManagementRing();
//        ManagerAddress coordinator = managementRing.addNewCoordinator();
//        coordinator.setSoapAddress("http://linbox3.extreme.indiana.edu:4567/manager");
//        coordinator.setRank(3);
//        ManagerData managerData = managementRing.addNewManagerData();
//        managerData.addNewManagerAddress().setSoapAddress("http://linbox3.extreme.indiana.edu:4568/manager");
//        for(int i=0;i<10;i++){
//            ResourceOutline resourceData = managerData.addNewResourceData();
//            resourceData.setName(new QName("http://linbox3.extreme.indiana.edu/test","LeadService"+i).toString());
//            resourceData.setState(SystemState.UpState.toString());
//        }
//        
//        XmlCursor newCursor = managerContextDocument.newCursor();
//        newCursor.toStartDoc();
//        newCursor.toFirstChild();
//        newCursor.insertProcInst("xml-stylesheet", "type=\"text/xsl\" href=\"resources/xslt/system.xslt\"");
//        newCursor.dispose();
//        
//        FileWriter fileWriter = new FileWriter("4.xml");
//        managerContextDocument.save(fileWriter);
//        fileWriter.close();
        ManagerContextDocument managerContextDocument = ManagerContextDocument.Factory.parse(new File("4.xml"));
        FileOutputStream outstream = new FileOutputStream("1.html");
        transformWithXSLT(managerContextDocument, "resources/xslt/system.xslt", outstream);
        outstream.close();
        System.out.println(Utils.readFile("1.html"));
        
    }
}
